package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModelProviders
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.ui.activity.LoginActivity
import com.cxh.rmsq.utils.ToastUtils
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import io.rong.callkit.util.SPUtils
import io.rong.imkit.feature.resend.ResendManager

class SexActivity : AppCompatActivity(), View.OnClickListener {

    var sex = -1
    private var userInfoViewModel: UserInfoViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sex)
        findViewById<ConstraintLayout>(R.id.cl_toolbar).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)
        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)
        findViewById<LinearLayout>(R.id.iv_back).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_next).setOnClickListener(this)
        findViewById<RadioGroup>(R.id.radio_group).setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_boy -> {
                    sex = 1
                }

                R.id.rb_girl -> {
                    sex = 2
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                    ResendManager.getInstance().removeAllResendMessage()
                    logout(false)
                    // 通知退出
                    sendLogoutNotify()
                    SPUtils.clear(applicationContext)
                    com.blankj.utilcode.util.SPUtils.getInstance().clear()
                    startActivity(Intent(this, LoginActivity::class.java))
                    finish()
            }
            R.id.tv_next -> {
                if (sex != -1) {
                    val intent = Intent(this@SexActivity, CompleteDataActivity::class.java)
                    intent.putExtra(Constant.SEX, sex)
                    startActivity(intent)
                }else{
                    ToastUtils.showToast("请选择你的性别")
                }
            }
        }
    }


    private fun logout(isAccountDeleted: Boolean) {
        if (userInfoViewModel != null) {
            if (isAccountDeleted) {
                userInfoViewModel!!.accountDelete()
            } else {
                userInfoViewModel!!.logout()
            }
        }
    }

    /** 通知通其他注册了登出广播的 Activity 关闭  */
    fun sendLogoutNotify() {
        // 发送广播
        val intent = Intent("com.rong.im.action.logout")
        sendBroadcast(intent)
    }
}